var express = require("express");
var router = express.Router();
var query = require("../conn/conn");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});
router.get("/index/recommend", async function (req, res, next) {
  console.log(req.query);
  let count = req.query?.count;
  let results = await query(`select * from recommend limit 0,${count || 1}`);
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
    const rest = results.map(item => {
        return {...item, picure: JSON.parse(item.picure),label:JSON.parse(item.label)}
    })
    
    console.log(rest)
    
  res.json({ status: 0, count: count, msg: "请求成功", result: rest });
});

router.get("/index/exhibition", async (req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    let count = req.query?.count;
    let results = await query(`select * from exhibition limit 0,${count || 1}`);

    results.forEach((item)=>{
        // item.price=random(55,290);
        item.type=random(1,7);
        item.open=random(0,2);
    });
    const resu=results.map(item=>{
        return {...item,ticketinfo:JSON.parse(item.ticketinfo)}
    })
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
  res.send({ status: 0, count: count, msg: "请求成功", result: resu });
})

router.get('/index/ticketinfo',async (req,res,next)=>{
    let id=req.query.id;
    let type=req.query.type
    let typea;
    switch (type) {
        case '1':
            typea='exhibition';
            break;
        case '2':
            typea='showtime';
            break;
        case '3':
            typea='music';
            break;
        case '4':
            typea='ifc';
            break;
        case '5':
            typea='dance';
            break;
    }
    // res.send({type,typea,id,resu:(`SELECT * FROM ${typea} WHERE id='${id}'`)})
    let ress=await query(`SELECT * FROM ${typea} WHERE id='${id}'`);
    if(ress.length>0){
        let resu=ress.map(item=>{
            return {...item,ticketinfo:JSON.parse(item.ticketinfo)};
        });

        res.send({status:0,msg:"请求成功",result:resu})
    }else{
        res.send({status:1,msg:"参数错误"})
    }
})

router.get("/index/showtime", async (req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    console.log(req.query);
    let count = req.query?.count;
    let results = await query(`select * from showtime limit 0,${count || 1}`);
    let resu=results.forEach((item)=>{
        // item.price=random(550,2300);
        item.want=random(7758,15740);
        item.type=random(1,7);
        item.open=random(0,2);
    })
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
  res.send({ status: 0, count: count, msg: "请求成功", result: results });
})

router.get("/index/music", async (req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    console.log(req.query);
    let count = req.query?.count;
    let results = await query(`select * from music limit 0,${count || 1}`);
    let resu=results.forEach((item)=>{
        // item.price=random(550,2300);
        item.want=random(7758,15740);
        item.type=random(1,7);
        item.open=random(0,2);
    })
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
  res.send({ status: 0, count: count, msg: "请求成功", result: results });
})

router.get("/index/ifc", async (req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    console.log(req.query);
    let count = req.query?.count;
    let results = await query(`select * from ifc limit 0,${count || 1}`);
    let resu=results.forEach((item)=>{
        // item.price=random(550,2300);
        item.want=random(7758,15740);
        item.type=random(1,7);
        item.open=random(0,2);
    })
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
  res.send({ status: 0, count: count, msg: "请求成功", result: results });
})

router.get("/index/dance", async (req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    console.log(req.query);
    let count = req.query?.count;
    let results = await query(`select * from dance limit 0,${count || 1}`);
    let resu=results.forEach((item)=>{
        // item.price=random(550,2300);
        item.want=random(7758,15740);
        item.type=random(1,7);
        item.open=random(0,2);
    })
    if(req.query.count==""||req.query.count=="undefined"||req.query.count==undefined){
        return res.send({status:1,msg:"参数错误"})
    }
  res.send({ status: 0, count: count, msg: "请求成功", result: results });
})


router.get("/index/market",async(req,res,next)=>{
    function random(min,max){
        return Math.floor(Math.random()*(max-min))+ min;
    }
    let count =req.query?.count;
    let results=await query(`select * from market limit 0,${count || 1}`);
    results.forEach((item)=>{
        item.price=random(77,1000);
        item.likes=random(200,6884);
    })
    res.send({status:0,msg:"请求成功",result:results});
})

router.post('/index/tokenInfo',async (req,res,next)=>{
    let token=req.body?.token;
    if(token==undefined||token=="undefined"){
        res.send({status:1,msg:"参数错误"})
    }
    let ress=await query(`SELECT * FROM user WHERE token='${token}'`);
    let username=ress[0]?.user;
    res.send({status:0,msg:"请求成功",result:{username}});
})

module.exports = router;
